import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: ()=>import("@/views/layout"),
    children:[
      {
        path: '',
        name: 'home',
        component: ()=>import("@/views/home"),
      },
      {
        path: '/classify',
        name: 'classify',
        component: ()=>import("@/views/classify"),
      },
      {
        path: '/shop',
        name: 'shop',
        component: ()=>import("@/views/shop"),
      },
      {
        path: '/my',
        name: 'my',
        component: ()=>import("@/views/my"),
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: ()=>import("@/views/login")
  },
  {
    path: '*',
    name: '404',
    component: ()=>import("@/views/404")
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 访问的白名单, 可以直接通行
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 访问的路径在白名单
  let token=window.localStorage.getItem("tokens")
  if (!whiteList.includes(to.path)) {
    if(!!token){
    next()
    }else {
      router.push("/login")
    }
  } 
  next()
})

export default router
